FML 闭包数据库

用闭包改写了一下,倒是可以用

#!/usr/bin/python
# coding:cp936
# CopyRights 2012 Adou XD , All Rights Reserved .

import sqlite3

dbname = 'fmldb.db'

def dbnc(func):
    def wrapper(*args,**kw):
        global con,cur
        con = sqlite3.connect(dbname)
        cur = con.cursor()

        result = func(*args,**kw)

        cur.close()
        con.close()
        return result
    return wrapper

@dbnc
def createDb():
    '''create the blog datebase'''
    cur.execute('create table if not exists fmls (id integer primary key autoincrement,self.content text)')
    con.commit()

@dbnc
def getFmls():
    '''get all the fmls'''
    cur.execute('select * from fmls')
    allfmls = cur.fetchall()
    return allfmls

@dbnc
def getFml(id):
    '''get fml by id'''
    cur.execute( "select * from fmls where id=%d" % id )
    blogbyid =  cur.fetchone()
    return blogbyid 

@dbnc
def inFmldb(story):
    '''get fml by id'''
    cur.execute( "select count(*) from fmls where self.content='%s'" % (story) )
    isthere =  cur.fetchone()
    if isthere[0]==0 :
        return False
    else :
        return True

@dbnc
def newFml(newstory):
    '''add a new fml'''
    result = cur.execute( "insert into fmls (self.content) values ('%s')" % (newstory) )
    con.commit()
    return result 
    
@dbnc
def delById(idnum):
    '''delete fml by id'''
    result = cur.execute( "delete from fmls where id=%d" % idnum )
    con.commit()
    return result

@dbnc
def delBycontent(story):
    '''delete fml by id'''
    result = cur.execute( "delete from fmls where self.content=%s" % story )
    con.commit()
    return result


if __name__ == "__main__" :
    '''test the database'''
    #createDb()
    allfmls =  getFmls()
    #allfmls = getFml(55)
    #allfmls = [ allfmls ]

    for num,content in allfmls :
        print 
        print num,content